﻿using INSE6260.OnlineBanking.Model.Accounts;
using INSE6260.OnlineBanking.Repository.EF.Repositories;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WatiN.Core;

namespace INSE6260.OnlineBanking.Repository.EF.Test
{
    
    [TestClass]
    public class TransactionTest
    {
        

        #region Additional test attributes

        [ClassInitialize]
        public static void MyClassInitialize(TestContext testContext)
        {
           
        }

        private EFUnitOfWork _unitOfWork;
        [TestInitialize]
        public void Precondition()
        {
            DbContextFactory.SetupDbConnection("OnlineBanking");
            _unitOfWork = new EFUnitOfWork();
        }
        #endregion

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        [TestMethod]
        public void WithdrawTestSuccess()
        {
            using (var browser = new IE("http://localhost:28241/"))
            {
                DoLogin(browser);
                var accountRepository = new AccountRepository(_unitOfWork);
                var account = accountRepository.First(acc => acc.ClientId == 1 && acc is CheckingAccount);
                account.Balance = account.Balance + 10;
                accountRepository.SaveChanges();
                browser.Link(Find.ById("WithdrawLink")).Click();
                Assert.IsTrue(browser.ContainsText("Money Card:"));
                browser.TextField(Find.ByName("Amount")).TypeText("10");
                browser.Button(Find.ByValue("Confirm")).Click();
                Assert.IsTrue(browser.ContainsText("Transaction was performed successfully"));
                account.Balance = account.Balance - 10;
                accountRepository.SaveChanges();
            }
        }
        [TestMethod]
        public void WithdrawInsufientAmount()
        {
            using (var browser = new IE("http://localhost:28241/"))
            {
                DoLogin(browser);
                var accountRepository = new AccountRepository(_unitOfWork);
                var account = accountRepository.First(acc => acc.ClientId == 1 && acc is CheckingAccount);
                browser.Link(Find.ById("WithdrawLink")).Click();
                Assert.IsTrue(browser.ContainsText("Money Card:"));
                browser.TextField(Find.ByName("Amount")).TypeText((account.Balance + 100).ToString());
                browser.Button(Find.ByValue("Confirm")).Click();
                Assert.IsTrue(browser.ContainsText("Insufficient Funds"));
            }
        }
        [TestMethod]
        public void WithdrawAmountGreaterThanWPD()
        {
            using (var browser = new IE("http://localhost:28241/"))
            {
                DoLogin(browser);
                var accountRepository = new AccountRepository(_unitOfWork);
                var account = accountRepository.First(acc => acc.ClientId == 1 && acc is CheckingAccount);
                account.Balance = account.Balance + account.WPD;
                accountRepository.SaveChanges();
                browser.Link(Find.ById("WithdrawLink")).Click();
                Assert.IsTrue(browser.ContainsText("Money Card:"));
                browser.TextField(Find.ByName("Amount")).TypeText((account.WPD + 10).ToString());
                browser.Button(Find.ByValue("Confirm")).Click();
                Assert.IsTrue(browser.ContainsText("Amount is more than WPD"));
                account.Balance = account.Balance - account.WPD;
                accountRepository.SaveChanges();
            }
        }

        [TestMethod]
        public void WithdrawAmountGreaterThanWPT()
        {
            using (var browser = new IE("http://localhost:28241/"))
            {
                DoLogin(browser);
                var accountRepository = new AccountRepository(_unitOfWork);
                var account = accountRepository.First(acc => acc.ClientId == 1 && acc is CheckingAccount);
                account.Balance = account.Balance + account.WPT;
                var accountWPD = account.WPD;
                account.WPD = account.WPT + 12;
                accountRepository.SaveChanges();
                browser.Link(Find.ById("WithdrawLink")).Click();
                Assert.IsTrue(browser.ContainsText("Money Card:"));
                browser.TextField(Find.ByName("Amount")).TypeText((account.WPT + 10).ToString());
                browser.Button(Find.ByValue("Confirm")).Click();
                Assert.IsTrue(browser.ContainsText("Amount is more than WPT"));
                account.Balance = account.Balance - account.WPT;
                account.WPD = accountWPD;
                accountRepository.SaveChanges();
            }
        }

        [TestMethod]
        public void WithdrawAmountIsEmpty()
        {
            using (var browser = new IE("http://localhost:28241/"))
            {
                DoLogin(browser);
                browser.Link(Find.ById("WithdrawLink")).Click();
                Assert.IsTrue(browser.ContainsText("Money Card:"));
                browser.TextField(Find.ByName("Amount")).TypeText("");
                browser.Button(Find.ByValue("Confirm")).Click();
                Assert.IsTrue(browser.ContainsText("The Amount field is required."));
                
            }
        }
        private void DoLogin(IE browser)
        {
            
            browser.TextField(Find.ByName("CardNo")).TypeText("123456789000");
            browser.TextField(Find.ByName("Password")).TypeText("123");
            browser.Button(Find.ByValue("SignIn")).Click();
            if (browser.ContainsText("name of your first pet"))
                browser.TextField(Find.ByName("Answer")).TypeText("TT");
            if (browser.ContainsText("Name of your oldest sibling"))
                browser.TextField(Find.ByName("Answer")).TypeText("ali");
            if (browser.ContainsText("Your first employer"))
                browser.TextField(Find.ByName("Answer")).TypeText("m");
            browser.Button(Find.ByValue("Ok")).Click();
            Assert.IsTrue(browser.ContainsText("Shahla Noori ") && browser.ContainsText("Branch Name: Concordia University 2011/2012"));
        }
    }
}